package com.edais.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.edais.service.UserService;

public class StrUtil {
	private static final Logger logger = Logger.getLogger(StrUtil.class);
	
	/**
	 * email隐藏中间位处理
	 */
	public static String hiddenEmailContent(String email){
		if(StringUtils.isBlank(email)){
			return email;
		}
		StringBuffer buff = new StringBuffer();
		
		String[] emailResult = email.split("@");
		if(emailResult.length < 2){
			return email;
		}
		
		for(int i= 0 ; i< emailResult[0].length(); i++ ){
			String tmp = emailResult[0].substring(i, i+1);
			if(i != 0 && i != emailResult[0].length()-1){
				tmp = "*";
			}
			buff.append(tmp);
		}
		String newEmail = buff.toString() + "@" + emailResult[1];
		return newEmail;
	}
	
	/**
	 * 手机隐藏中间4位处理
	 */
	public static String hiddenMobileContent(String mobile){
		if(StringUtils.isBlank(mobile) || mobile.length() < 11){
			return mobile;
		}
		//return mobile.replaceAll(mobile.substring(3,4),"****");
		return mobile.substring(0,mobile.length()-(mobile.substring(3)).length())+"****"+mobile.substring(7);
	}
	
	/**
	 * 银行卡位数检查
	 */
	public static boolean bankCardCheck(String bankCard){
		if(StringUtils.isBlank(bankCard)){
			return false;
		}
		boolean result=bankCard.matches("\\d{13,19}");
		return result;
	}
	
	/**
	 * 发送标识检查
	 * @param args
	 */
	public static boolean setupFlagCheck(String setupFlag){
		if(!("1".equals(setupFlag) || "2".equals(setupFlag))){
			return false;
		}
		return true;
	}
	
	  public static String fileNameMD5(String fileName)throws Exception{
	    	if(fileName==null){
	    		fileName="";
	    	}
	    	String filename_new = "gefhdbca" + fileName + "acbdhfeg";
	    	try {
				if (StringUtils.isBlank(fileName)) {
					return MD5.md5(filename_new);
				} else {
					if(fileName.length()>=2){
						String str=fileName.substring(fileName.length()-2);
						System.out.println(str);
						filename_new=str+filename_new;
					}
					return MD5.md5(filename_new);
				}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				return MD5.md5(filename_new);
			}
	    }
	  
	  /**
		 * 根据时间区间对应不同的协议编号
		 * @param mat
		 * @param orderDate
		 * @return
		 * @throws Exception
		 */
	  public static Map<String,String> getAgreementNo(UserService userService,String mat,String orderDate) throws Exception{
		  Map<String,String> reMap=new HashMap<String,String>();
		  Map<String,String> repMap=null;
		  String loan_agreement_no="";
		  String loan_content_agreement_no="";
		  try{
			  List<Map<String, String>> listMap = userService.getSysSettByAgreementNo();
			  for (Map<String, String> map : listMap) {
				  reMap.put(map.get("NAME"),map.get("VALUE"));
			  }
			  for (Iterator<String> iterator = reMap.keySet().iterator(); iterator.hasNext();) {
				  String type =  iterator.next();
				  String value=reMap.get(type);
				  logger.debug("++++++++++++++++++++++type="+type);
				  logger.debug("++++++++++++++++++++++value="+value);
				  String[] items=null;

				  if(value!=null&&!"".equals(value)&&
						  value.lastIndexOf("|")!=-1){
					  items=value.split("\\|");
				  }else{
					  items=new String[]{"0","0","0"};
				  }
				  if(Integer.parseInt(orderDate)>=Integer.parseInt(items[0])
						  &&Integer.parseInt(orderDate)<=Integer.parseInt(items[1])){
					  if(type!=null&&type.startsWith("loan_agreement_no_"+mat+"_")){
						  loan_agreement_no=items[2];
					  }
					  if(type!=null&&type.startsWith("loan_content_agreement_no_"+mat+"_")){
						  loan_content_agreement_no=items[2];
					  }
				  }
				  if(!"".equals(loan_agreement_no)&&!"".equals(loan_content_agreement_no)){
					  break;
				  }
			  }
		  } catch (Exception e) {
			  // TODO: handle exception
			  e.printStackTrace();
			  logger.debug("+++++++getAgreementNo+++++++++++"+e.getMessage());
			  return repMap;
		  }
		  logger.debug("++++++++++++++++++++++loan_agreement_no="+loan_agreement_no);
		  logger.debug("++++++++++++++++++++++loan_content_agreement_no="+loan_content_agreement_no);
		  repMap=new HashMap<String,String>();
		  repMap.put("loan_agreement_no", loan_agreement_no);
		  repMap.put("loan_content_agreement_no", loan_content_agreement_no);
		  return repMap;
	  }

	public static void main(String[] args) throws Exception{
		//boolean result = bankCardCheck("123456789012345678*");
		//System.out.println("result=" + result);
		
		System.out.println(hiddenMobileContent("13718401770"));
		System.out.println(hiddenMobileContent("13683604700"));
		System.out.println(hiddenMobileContent("15010220017"));
		System.out.println(hiddenMobileContent("13718401770"));
		String ss="101330400";
    	System.out.println(fileNameMD5(ss));
    	String ss1="101330401";
    	System.out.println(fileNameMD5(ss1));
      	String ss2="101330402";
    	System.out.println(fileNameMD5(ss2));
       	String ss3="101330403";
    	System.out.println(fileNameMD5(ss3));
		
	}

}
